var port = location.port,
    protocol = location.protocol;
layui.define(['jquery', 'element', 'layer'], function(e) {
    "use strict";
    var $ = layui.$,
        layer = layui.layer,
        element = layui.element;

    // ajax请求全局参数
    $.ajaxSetup({
        type: 'POST',
        dataType: 'json'
    });
    // 顶部条
    var fontPage = '',
        topItem_line = '<li><span class="v-line"></span></li>',
        topItem_1 = '<li class="userdo"><a href="/sso/login">登录</a> / <a href="/sso/register">注册</a></li>',
        topItem_2 = '<li><a href="' + MEMBER_URL + '/member/message">我的消息</a></li>',
        topItem_3 = '<li data-hover-cls="top-menu-hover"><a href="' + MEMBER_URL + '/member/customer">买家中心<i class="top-arrow"></i></a><div class="top-menu-mask"></div><div class="top-menu-box top-menu-shadow"><ul><li><a href="' + MEMBER_URL + '/member/customer/shoporder">已买到商品</a></li><li><a href="' + MEMBER_URL + '/member/customer/manage">询价管理</a></li><li><a href="' + MEMBER_URL + '/member/customer/myfooter">我的足迹</a></li></ul></div></li>',
        topItem_4 = '<li data-hover-cls="top-menu-hover"><a href="' + MEMBER_URL + '/member/merchant">卖家中心<i class="top-arrow"></i></a><div class="top-menu-mask"></div><div class="top-menu-box top-menu-shadow"><ul class=""><li><a href="' + MEMBER_URL + '/member/merchant/mygoods">我的商品</a></li><li><a href="' + MEMBER_URL + '/member/merchant/myorder">订单管理</a></li><li><a href="' + MEMBER_URL + '/member/merchant/publish">报价管理</a></li><li><a href="' + MEMBER_URL + '/member/store/index">我的店铺</a></li></ul></div></li>',
        topItem_5 = '<li><a href="' + HELP_URL + '">帮助中心</a></li>';
    var fixedItem_1 = '<div class="fixed-bar-item" data-hover-cls="hover"><a href="' + MEMBER_URL + '/member/customer/publish" class="fixed-bar-link"><span class="fixed-bar-title">发布询价单</span><i class="icon-fixed icon-fixed-1"></i></a></div>',
        fixedItem_2 = '<div class="fixed-bar-item" data-hover-cls="hover"><a href="" class="fixed-bar-link"><span class="fixed-bar-title">免费求购</span><i class="icon-fixed icon-fixed-2"></i></a></div>',
        fixedItem_3 = '<div class="fixed-bar-item" data-hover-cls="hover"><a href="'+ MALL_URL +'/ordinary/cart" class="fixed-bar-link"><span class="fixed-bar-title">购物车</span><i class="icon-fixed icon-fixed-3"></i></a></div>',
        fixedItem_4 = '<div class="fixed-bar-item" data-hover-cls="hover"><a href="http://wpa.qq.com/msgrd?v=3&uin=2112574008&site=qq&menu=yes" class="fixed-bar-link"><span class="fixed-bar-title">在线客服</span><i class="icon-fixed icon-fixed-4"></i></a></div>',
        fixedItem_5 = '<div class="fixed-bar-item layui-hide" data-hover-cls="hover" id="js_backtop"><a href="javascript:;" class="fixed-bar-link"><span class="fixed-bar-title">返回顶部</span><i class="icon-fixed icon-fixed-5"></i></a></div>';
    var global = {
        myAjax:function(){
            if (location.href.indexOf(INDEX_URL)>-1) {
                return {
                    url: '/index/getUinfo',
                    type:'get'
                }
            } else {
                return {
                    url: INDEX_URL + '/index/getUinfo',
                    type:'get',
                    dataType: 'jsonp',
                    jsonp:'jsoncallback'
                }
            }
        },
        getUserInfo: function(){
            $.ajax(global.myAjax())
            .done(function(res) {
                if (res.code == 1) {
                    var data = res.data;
                    // 浮窗显示
                    if (data.cartNum > 0) {// 浮窗购物车
                        fixedItem_3 = '<div class="fixed-bar-item" data-hover-cls="hover"><a href="'+ MALL_URL +'/ordinary/cart" class="fixed-bar-link"><span class="fixed-bar-title">购物车</span><i class="icon-fixed icon-fixed-3"></i><b class="fixed-cart-num">'+ data.cartNum +'</b></a></div>';
                    }
                    if ($(window).scrollTop() > 500) {
                        fixedItem_5 = '<div class="fixed-bar-item" data-hover-cls="hover" id="js_backtop"><a href="javascript:;" class="fixed-bar-link"><span class="fixed-bar-title">返回顶部</span><i class="icon-fixed icon-fixed-5"></i></a></div>';
                    }
                    if (data.uinfo.special_status != 4 && data.uinfo.special_status != 5) {
                        $('body').append('<div class="web-fixedbar">'+ fixedItem_1 + fixedItem_2 + fixedItem_3 + fixedItem_4 + fixedItem_5 +'</div>');
                    }
                    // banner右方状态栏处理
                    if (data.uinfo.headImg != '') {
                        $("#screen-user-icon").attr('src', data.uinfo.headImg);
                    }
                    var userHtml = '';
                    if (data.uinfo.special_status == 1) {
                        userHtml = '<a class="user-login" href="' + MEMBER_URL + '/member/center">'+ data.uinfo.mobilePhone+'</a><a href="' + MEMBER_URL + '/member/certify/person" class="link-rz">成为认证会员，享受平台权益 <i class="icon-torz"></i></a>';
                        $('.user-wrap').addClass('user-wrap-pad');
                    } else {
                        userHtml = '<p><span>' + data.uinfo.realname + '</span><em class="icon-verified">已认证</em></p><a class="user-login" href="' + MEMBER_URL + '/member/center">'+ data.uinfo.mobilePhone+'</a>'
                    }
                    $("#screen-user-box").html(userHtml);
                    // 顶部状态栏处理
                    fontPage = '<li><a href="' + INDEX_URL + '">木头云首页</a></li><li><span class="v-line"></span></li>'
                    topItem_1 = '<li class="userlogin"><a href="' + MEMBER_URL + '/member/center">' + data.uinfo.realname + '</a> [<a href="/sso/loginOut">退出</a>]</li>';
                    if (data.messageNum > 0) {
                        topItem_2 = '<li><a href="' + MEMBER_URL + '/member/message" class="fs_0"><span class="layui-inline fs_14">我的消息</span><span class="layui-badge layui-inline top_info_number">' + data.messageNum + '</span></a></li>';
                    }
                    if (data.uinfo.special_status == 4) {
                        topItem_3 = '<li data-hover-cls="top-menu-hover"><a href="' + HOME_URL + '/member/customer">买家中心<i class="top-arrow"></i></a><div class="top-menu-mask"></div><div class="top-menu-box top-menu-shadow"><ul class=""><li><a href="' + MEMBER_URL + '/member/customer/specialorder">精选商品</a></li><li><a href="' + MEMBER_URL + '/member/customer/buyback">收回服务</a></li><li><a href="' + MEMBER_URL + '/member/contract/list">我的合同</a></li></ul></div></li>';
                        $('#topArea').html(fontPage + topItem_1 + topItem_line + topItem_2 + topItem_line + topItem_3 + topItem_line + topItem_5);
                        return;
                    } else if (data.uinfo.special_status == 5) {
                        topItem_4 = '<li data-hover-cls="top-menu-hover"><a href="' + MEMBER_URL + '/member/merchant">卖家中心<i class="top-arrow"></i></a><div class="top-menu-mask"></div><div class="top-menu-box top-menu-shadow"><ul><li><a href="' + MEMBER_URL + '/member/specialproduct/publish">发布精选</a></li><li><a href="' + MEMBER_URL + '/member/specialproduct">精选订单</a></li><li><a href="' + MEMBER_URL + '/member/specialproduct/backlist">返还计划</a></li><li><a href="' + MEMBER_URL + '/member/contract/mylist">我的合同</a></li></ul></div></li>';
                        $('#topArea').html(fontPage + topItem_1 + topItem_line + topItem_2 + topItem_line + topItem_4 + topItem_line + topItem_5);
                        return;
                    }
                } else {
                    $('body').append('<div class="web-fixedbar">'+ fixedItem_1 + fixedItem_2 + fixedItem_3 + fixedItem_4 + fixedItem_5 +'</div>');
                }
                $('#topArea').html(fontPage + topItem_1 + topItem_line + topItem_2 + topItem_line + topItem_3 + topItem_line + topItem_4 + topItem_line + topItem_5)
            })
            .fail(function() {
                layer.msg("用户信息获取失败，请刷新页面！", {icon:5});
            })
            .always(function() {
                console.log("complete");
            });
        },

    }
    global.getUserInfo();
    function backTop(){
        var scrollTop = $(window).scrollTop();
        if (scrollTop >= 500) {
            $(document).find('#js_backtop').removeClass('layui-hide');
        } else {
            $(document).find('#js_backtop').addClass('layui-hide');
        }
    }
    $(document).on('scroll', function(e) {
        backTop();
    });
    $(document).on('click', '#js_backtop', function(e) {
        e.preventDefault();
        $('body,html').animate({scrollTop: 0},300);
    });
    // 全局鼠标经过
    $(document).on('mouseover', '[data-hover-cls]', function(event) {
        event.preventDefault();
        var hoverClass = $(this).data('hover-cls');
        $(this).addClass(hoverClass)
    }).on('mouseout', '[data-hover-cls]', function(event) {
        event.preventDefault();
        var hoverClass = $(this).data('hover-cls');
        $(this).removeClass(hoverClass)
    });
    /* 头部搜索 */
    $('.menu-search-opt').on('click', '.menu-search-type', function(event) {
        event.preventDefault();
        var $parent = $(this).parent();
        var $searchbox = $parent.siblings('.menu-searching');
        var aType = $(this).data("type");
        var aTxt = $(this).text();
        if (aType == 2) {
            $searchbox.attr('data-type', 2).find('span').text(aTxt);
            $parent.html('<div class="menu-search-type" data-type="1">商品</div>');
            $('#searchInput').attr('placeholder', '请输入店铺名称进行搜索');
            $('#searchType').val(2);

        } else if (aType == 1) {
            $searchbox.attr('data-type', 1).find('span').text(aTxt);
            $parent.html('<div class="menu-search-type" data-type="2">店铺</div>');
            $('#searchInput').attr('placeholder', '请输入品类 品名 规格 产地进行搜索');
            $('#searchType').val(1);
        }
        $parent.parent().removeClass('hover');
    });
    e('global', global);
});
